<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>闭包精细</title>
	</head>
	<body>
		<script type="text/javascript">
			/**
			function test(){
				var temp = 100;
				function a(){
					console.log(temp);
				}
				return a;
			}
			var demo = test();
			demo();
			*/
			
			/**
			function demo(){
				var arr = [];
				for (var i = 0; i < 10; i++) {
					(function(j){
						arr[i] = function(){
						console.log(j + "");
						}
					}(i));
				}
				return arr;
			}
			
			var fun = demo();
			for (var i = 0; i < 10; i++) {
				fun[i]();
			}
			
			*/

			//函数累加器
			/**
			function add(){
				var sum = 0;
				function sumfun(){
					sum++;
					console.log(sum);
				}
				return sumfun;
			}
			
			var demo = add();
			demo();
			*/
			
			function test(){
				var food = 'apple';
				var obj = {
					eatFood : function(){
						if(food != ""){
							console.log("I am eating " + food);
							food = "";
						}
					},
					pushFood : function(myFood){
						food = myFood;
					}
				}
				
				return obj;
			}
			
			var people = test();
			people.eatFood();
			people.pushFood('banana');
			people.eatFood();
		</script>
	</body>
</html>
		